package global.com.loansapplication.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.superrecycleview.superlibrary.recycleview.ProgressStyle;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import global.com.loansapplication.R;
import global.com.loansapplication.base.BaseFragment;
import global.com.loansapplication.bean.BatchInfo;
import global.com.loansapplication.bean.ClueInfo;
import global.com.loansapplication.bean.UserInfo;
import global.com.loansapplication.event.NetErrorEvent;
import global.com.loansapplication.ui.adapter.OwnAdapter;
import global.com.loansapplication.ui.fragment.biz.CustomClueBiz;
import global.com.loansapplication.ui.fragment.component.DaggerCustomClueComponent;
import global.com.loansapplication.ui.fragment.contract.CustomClueContract;
import global.com.loansapplication.ui.fragment.module.CustomClueModule;
import global.com.loansapplication.ui.fragment.presenter.CustomCluePresent;
import rx.functions.Action1;

public class CustomClueFrag extends BaseFragment implements CustomClueContract.View, SuperRecyclerView.LoadingListener {

    @Inject
    CustomCluePresent mPresenter;
    @Inject
    CustomClueBiz mBiz;

    private OwnAdapter mAdapter;

    @Bind(R.id.tv_add_count)
    TextView mTvAddCount;
    @Bind(R.id.clue_last)
    TextView mTvClueLast;
    @Bind(R.id.tv_export)
    TextView mTvExport;
    @Bind(R.id.image_apply_more)
    ImageView mImageApplyMore;

    @Bind(R.id.image_no_data)
    ImageView mImageNoData;

    @Bind(R.id.super_recycler_view)
    SuperRecyclerView superRecyclerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_custom_clue, container, false);
        ButterKnife.bind(this, view);

        DaggerCustomClueComponent.builder().customClueModule(new CustomClueModule(this)).build().inject(this);

        mPresenter.setBiz(mBiz);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        superRecyclerView.setLayoutManager(layoutManager);
        superRecyclerView.setRefreshEnabled(true);// 可以定制是否开启下拉刷新
        superRecyclerView.setLoadMoreEnabled(true);// 可以定制是否开启加载更多
        superRecyclerView.setLoadingListener(this);// 下拉刷新，上拉加载的监听
        superRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);// 下拉刷新的样式
        superRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);// 上拉加载的样式
        superRecyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);// 设置下拉箭头

        RxView.clicks(mTvAddCount)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        mPresenter.toApplyCustom();
                    }
                });

        RxView.clicks(mTvExport)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        mPresenter.toExportData();
                    }
                });

        RxView.clicks(mImageApplyMore)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        mPresenter.toApplyMore();
                    }
                });

        return view;
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    public void onResume() {
        mPresenter.getUserInfo();
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mBiz.onDestory();
        ButterKnife.unbind(this);
    }

    @Override
    public void updateUI(UserInfo userInfo) {
        if (userInfo.getUseable().equals("1")) {
            mImageApplyMore.setVisibility(View.GONE);
            mTvClueLast.setText(userInfo.getThreadPool());
            mPresenter.getBatchInfoList();
        } else {
            mImageApplyMore.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void refreshComplete() {
        superRecyclerView.completeRefresh();
    }

    @Override
    public void loadMoreComplete() {
        superRecyclerView.completeLoadMore();
    }

    @Override
    public void hasLoadMore(boolean b) {
        superRecyclerView.setNoMore(b);
    }

    @Override
    public void updateList(List<ClueInfo> coupons) {
        if (mAdapter == null) {
            mAdapter = new OwnAdapter(getActivity(), coupons, true);
            superRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void setNoDate(boolean b) {
        if (b) {
            mImageNoData.setVisibility(View.VISIBLE);
        } else {
            mImageNoData.setVisibility(View.GONE);
        }
    }

    @Override
    public void onRefresh() {
        mPresenter.refresh();
        mPresenter.getBatchInfoList();
    }

    @Override
    public void onLoadMore() {
        mPresenter.getBatchInfoList();
    }
}
